package demo2;

class Money implements Cloneable{
    public double money = 12.2;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

class Person extends Money implements Cloneable {
    public String name;
    public Money m;

    public Person(String name) {
        this.name = name;
        m = new Money();
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Person tmp = (Person)super.clone();
        tmp.m = (Money)this.m.clone();
        return tmp;
    }
}

public class Test2 {
    public static void main(String[] args) throws CloneNotSupportedException{
        Person person1 = new Person("zhangsan");
        Person person2 = (Person) person1.clone();
        System.out.println("修改前："+person1.m.money);
        System.out.println("修改前："+person2.m.money);
        person2.m.money = 88.66;
        System.out.println("修改后："+person1.m.money);
        System.out.println("修改后："+person2.m.money);

    }
    public static void main1(String[] args) throws CloneNotSupportedException {
        Person person1 = new Person("zhangsan");
        Person person2 = (Person) person1.clone();
        System.out.println(person2.name);
    }
}
